package 算法.力扣;

/**
 * @author: 谢君臣
 * @create: 2020-09-08 21:09
 * @founction: XXXX
 */
public class FindMid {

    public static void main(String[] args) {
        int[] num1 = new int[]{1, 2,5};
        int[] num2 = new int[]{3, 4};
        System.out.println(findMedianSortedArrays(num1, num2));
    }

    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int i = 0, j = 0, t = 0;
        int len = nums1.length + nums2.length;
        int[] num = new int[len];
        while (i < nums1.length && j < nums2.length) {
            if (nums1[i] > nums2[j])
                num[t++] = nums2[j++];
            else
                num[t++] = nums1[i++];
        }
        while (t <= (len) / 2) {
            if (i >= nums1.length) num[t++] = nums2[j++];
            else num[t++] = nums1[i++];
        }
        if ((len) % 2 != 0) return num[(len) / 2];
        else return (num[(len) / 2] + num[(len) / 2 - 1]) / 2.0;
    }
}
